package com.example.oscarchen.myapplication;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.oscarchen.myapplication.entity.Word;
import com.example.oscarchen.myapplication.viewmodel.WordViewModel;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    RecyclerView mRecyclerView;
    FloatingActionButton mFabNewWord;
    FloatingActionButton mFabChronometer;
    FloatingActionButton mFabDatabind;

    private WordViewModel mWordViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFabNewWord = findViewById(R.id.fab_new_word);
        mFabNewWord.setOnClickListener(this);

        mFabChronometer = findViewById(R.id.fab_chronometer);
        mFabChronometer.setOnClickListener(this);

        mFabDatabind = findViewById(R.id.fab_databind);
        mFabDatabind.setOnClickListener(this);

        mRecyclerView = findViewById(R.id.recycler_view);
        final WordListAdapter mWordListAdapter = new WordListAdapter(null);

        mRecyclerView.setAdapter(mWordListAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mWordViewModel = ViewModelProviders.of(this).get(WordViewModel.class);
        mWordViewModel.getAllWords().observe(this, new Observer<List<Word>>() {
            @Override
            public void onChanged(@Nullable List<Word> words) {
                mWordListAdapter.setData(words);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fab_new_word:
                Intent newWordIntent = new Intent(MainActivity.this, NewWordActivity.class);
                startActivityForResult(newWordIntent, NEW_WORD_ACTIVITY_REQUEST_CODE);
                break;
            case R.id.fab_chronometer:
                Intent chronometerIntent = new Intent(MainActivity.this, StartActivity.class);
                startActivityForResult(chronometerIntent, NEW_WORD_ACTIVITY_REQUEST_CODE);
                break;
            case R.id.fab_databind:
                Intent databindIntent = new Intent(MainActivity.this, StartActivity.class);
                startActivityForResult(databindIntent, NEW_WORD_ACTIVITY_REQUEST_CODE);
                break;
        }
    }

    public class WordListAdapter extends BaseQuickAdapter<Word, BaseViewHolder> {

        public WordListAdapter(@Nullable List<Word> data) {
            super(R.layout.recyclerview_item, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Word item) {
            helper.setText(R.id.word_text, item.getWord())
                    .setText(R.id.word_id, item.getId() + ".");
        }

        public void setData(List<Word> mData) {
            this.mData = mData;
            notifyDataSetChanged();
        }
    }

    public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            Word word = new Word(data.getStringExtra(NewWordActivity.EXTRA_REPLY));
            mWordViewModel.insert(word);
        } else {
            Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_LONG).show();
        }
    }
}
